package com.ruixun.controller;

import com.ruixun.common.PageData;
import com.ruixun.common.ResponseResult;
import com.ruixun.entity.Role;
import com.ruixun.query.RolePageQuery;
import com.ruixun.rvo.RoleRvo;
import com.ruixun.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 角色管理控制器
 * @author ruixun
 */
@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/role")
@RequiredArgsConstructor
@Slf4j
public class RoleController {

    private final RoleService roleService;

    @ApiOperation(value = "分页查询角色列表", notes = "支持按角色名称模糊查询和角色类型等值查询")
    @PostMapping("/page")
    public ResponseResult<PageData<RoleRvo>> getRolePage(@Valid @RequestBody RolePageQuery query) {
        PageData<RoleRvo> pageData = roleService.getRolePage(query);
        return ResponseResult.success(pageData);
    }

    @ApiOperation(value = "新增角色", notes = "新增角色信息")
    @PostMapping("/add")
    public ResponseResult<Boolean> addRole(@Valid @RequestBody Role role) {
        boolean result = roleService.addRole(role);
        return result ? ResponseResult.success(true) : ResponseResult.fail("新增角色失败");
    }

    @ApiOperation(value = "修改角色", notes = "修改角色信息")
    @PostMapping("/update")
    public ResponseResult<Boolean> updateRole(@Valid @RequestBody Role role) {
        if (role.getId() == null) {
            return ResponseResult.fail("角色ID不能为空");
        }
        boolean result = roleService.updateRole(role);
        return result ? ResponseResult.success(true) : ResponseResult.fail("修改角色失败");
    }

    @ApiOperation(value = "删除角色", notes = "根据ID删除角色（逻辑删除）")
    @PostMapping("/delete/{id}")
    public ResponseResult<Boolean> deleteRole(@ApiParam(value = "角色ID", required = true) @PathVariable Long id) {
        boolean result = roleService.deleteRole(id);
        return result ? ResponseResult.success(true) : ResponseResult.fail("删除角色失败");
    }

    @ApiOperation(value = "获取角色详情", notes = "根据ID获取角色详细信息")
    @GetMapping("/detail/{id}")
    public ResponseResult<RoleRvo> getRoleDetail(@ApiParam(value = "角色ID", required = true) @PathVariable Long id) {
        RoleRvo role = roleService.getRoleById(id);
        return role != null ? ResponseResult.success(role) : ResponseResult.fail("角色不存在");
    }

}